package com.Bigbluebook.ViewMode;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.Bigbluebook.Service.CommentApiService;
import com.Bigbluebook.entity.Comment;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class CommentViewModel extends AndroidViewModel {
    private MutableLiveData<List<Comment>> commentListLiveData;

    public CommentViewModel(@NonNull Application application) {
        super(application);
        commentListLiveData = new MutableLiveData<>();
    }

    public void getComments(int userId) {
        CommentApiService commentService = new CommentApiService();
        commentService.getAllCommentsByUser(userId, new Callback<List<Comment>>() {
            @Override
            public void onResponse(Call<List<Comment>> call, Response<List<Comment>> response) {
                if (response.isSuccessful()) {
                    Log.d("---------------------", "userId: "+userId);
                    commentListLiveData.setValue(response.body());
                } else {
                    commentListLiveData.setValue(null); // 或者可以设置一个空列表
                }
            }

            @Override
            public void onFailure(Call<List<Comment>> call, Throwable t) {
                commentListLiveData.setValue(null); // 或者可以设置一个空列表
            }
        });
    }

    public LiveData<List<Comment>> getCommentListLiveData() {
        return commentListLiveData;
    }
}
